// pages/wxPay/wxPay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payParam: '',
    goodstype: 0,
    orderid: 0,
    orderprice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log('option:' + JSON.stringify(options));
    //let param = decodeURIComponent(options.payParam);
    // console.log(param);
    let payinfo = options;
    this.setData({
      goodstype: payinfo.goodstype,
      orderid: payinfo.orderid,
      orderprice: payinfo.orderprice
    })
    //console.log('payParam:' + param);
    //console.log('timeStamp:' + this.data.payParam.timeStamp);
    const _this = this;

    var my_access_token = getApp().core.getStorageSync('my_access_token');
    if (my_access_token){
        getApp().request({
          method: 'POST',
          data: { 'zsession': my_access_token, 'goodstype': payinfo.goodstype, 'orderid': payinfo.orderid},
          url: 'https://wxapp.yzhihe.com/wxappapi/?service=Payment.Dopaytest',
          success: function (res) {
            if (res.ret == 200) {
              console.log(res.data);
              _this.setData({
                payParam: res.data
              })
            }
          }
        });
    }else{
      /**
      * 会员登录
      */
      getApp().core.login({
        success: function (login_res) {
          var code = login_res.code;
          getApp().request({
            method: 'POST',
            data: { code: code },
            url: getApp().api.passport.wxlogin,
            success: function (res) {
              if (res.code == 0) {
                getApp().core.setStorageSync('my_access_token', res.data.access_token);
                console.log(res.data.access_token);
                var my_access_token = res.data.access_token;
                getApp().request({
                  method: 'POST',
                  data: { 'zsession': my_access_token, 'goodstype': payinfo.goodstype, 'orderid': payinfo.orderid },
                  url: 'https://wxapp.yzhihe.com/wxappapi/?service=Payment.Dopaytest',
                  success: function (res) {
                    if (res.ret == 200) {
                      console.log(res.data);
                      _this.setData({
                        payParam: res.data
                      })
                    }
                  }
                });
              }
            }
          });
        },
        fail: function (res) {
        },
      });

    }



  
    

  },

  calling: function () {
    wx.makePhoneCall({
      phoneNumber: '13381111987',
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  //采用小程序支付
  confirmPay: function () {
    console.log('wxpay');
    const _this = this;
    var goodstype = _this.data.goodstype;
    wx.requestPayment(
      {
        "timeStamp": _this.data.payParam.timeStamp,
        "package": _this.data.payParam.package,
        "appId": _this.data.payParam.appId,
        "signType": _this.data.payParam.signType,
        "nonceStr": _this.data.payParam.nonceStr,
        "paySign": _this.data.payParam.paySign,
         //支付成功的回调
        'success': function (res) {      
          console.log('success:' + JSON.stringify(res));
          if (goodstype == 1){
          getApp().core.redirectTo({
            url: "/pages/web/web?url=https://wxapp.yzhihe.com/wxappweb/ticket.html",
          });
          }
        },
        //支付失败的回调
        'fail': function (res) {
          console.log('fail:' + JSON.stringify(res));
          wx.showToast({
            //title: '支付失败:' + JSON.stringify(res),
            title: '支付失败',
            icon: 'none',
          })

        },
        'complete': function (res) {
          console.log('complete:' + JSON.stringify(res));
        }
      })
  }

})